<?php
/**
 * Created by PhpStorm.
 * User: sen
 * Date: 16/7/13
 * Time: 下午4:18
 */
namespace app\controllers;
use app\models\User;
use yii\web\Controller;
use app\models\Admin;
use app\models\Borkerage;

class CommissionController extends Controller{

    public function init(){
        $this->enableCsrfValidation = false;
        if(!\Yii::$app->user->id){
            $url = \Yii::$app->request->getPathInfo();
            return $this->redirect('../login/index?url='.$url);
        }
        $userId = \Yii::$app->user->id;
        $user = new User();
        $admin = $user->findIdentity($userId);
        if($admin['type']==1 or $admin['type']==0){
            return $this->redirect('../login/logout');
        }
    }


    //加载view
    public function actionSetCommission(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=41;

        $model=Admin::find()->where(['admin_id'=>1])->one();
        return $this->render('setCommission',['model' => $model]);
    }

    //佣金列表加载的View
    public function actionStrikeCommission(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=43;
        return $this->render('StrikeCommission');

    }

    //更新佣金比例
    public function actionUpdateCommission(){
        //获取相应字段
        $request = \Yii::$app->request;
        $first_rate = $request->get('first_rate');
        $second_rate = $request->get('second_rate');
        $third_rate = $request->get('third_rate');
        $admin = Admin::findOne(['admin_id' => 1]);
        $admin->first_rate = $first_rate;
        $admin->second_rate = $second_rate;
        $admin->third_rate = $third_rate;
        $rs=$admin->save();
        echo json_encode($rs);

    }

    // 佣金结算界面的 获取table的数据
    public function actionGetallgradedata(){

        //获取相应字段
        $request = \Yii::$app->request;
        $offset = $request->get('offset');
        $limit = $request->get('limit');
        $year= $request->get('year');
        $mon= $request->get('mon');

        $model = new Borkerage();
        $rs = $model->getYearMonthTableData($limit,$offset,$year,$mon);
        echo json_encode($rs);

    }


    public function actionSumallgrade($userid){

        $currentidary =  Borkerage::getorder($userid);
        $money = null;
        $borkermoney = null;
        $istransfer = null;
        foreach($currentidary as $key=>$value) {
            $money = $money + $value['orderform_money'];
            $borkermoney = $borkermoney + $value ['brokerage_money'];
            $istransfer = $istransfer + $value['is_transfer'];
        }

        if($istransfer > 0){
            $istransfer = 1;
        }else{
            $istransfer = 0;
        }

        return [
            'orderform_money' => $money,
            'brokerage_money' => $borkermoney,
            'is_transfer' => $istransfer,
        ];
    }

    public function actionStrikeallcommission(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=42;
        $model=Admin::find()->where(['admin_id'=>1])->one();
        return $this->render('strikeallCommission',['model' => $model]);
    }

    public function actionConfirmordes(){

        $currentId = $_GET['user_id'];
        Borkerage::updateAll(['is_transfer' => 1],['user_id' => $currentId]);
        return json_encode(['success' => true]);

    }

    //列表数据
    public function actionCommissionTableData(){

        //获取相应字段
        $request = \Yii::$app->request;
        $offset = $request->get('offset');
        $limit = $request->get('limit');
        $search = $request->get('search');

        $model = new Borkerage();
        $rs = $model->getTableData($limit,$offset,$search);
        echo json_encode($rs);

    }

    //打款操作
    public function actionTransferOperate(){

        //获取相应字段
        $request = \Yii::$app->request;
        $user_id = $request->get('user_id');
        $createTime = $request->get('createTime');
        $model = new Borkerage();
        $rs = $model->updateTransfer($user_id,$createTime);
        echo json_encode($rs);

    }


}